home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / ftpsubr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  3.5 KB  |  185 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "socket.h"
  5. #include "proc.h"
  6. #include "ftp.h"
  7. #include "ftpcli.h"
  8.  
  9. extern int16 Tcp_mss;
  10.  
  11. /* Send a file (opened by caller) on a network socket */
  12. long
  13. sendfile(fp,s,mode,hash)
  14. FILE *fp;    /* File to be sent */
  15. int s;        /* Socket to be sent on */
  16. int mode;    /* Transfer mode */
  17. int hash;    /* Print hash marks every Tcp_mss bytes */
  18. {
  19.     register struct mbuf *bp;
  20.     int c,oldf;
  21.     long total = 0;
  22.     long hmark = 0;
  23.  
  24.     if(hash == V_BYTE)
  25.         tprintf("         : Bytes Sent\r");
  26.     switch(mode){
  27.     default:
  28.     case LOGICAL_TYPE:
  29.     case IMAGE_TYPE:
  30.         for(;;){
  31.             if((bp = alloc_mbuf(BLKSIZE)) == NULLBUF)
  32.                 return -1;
  33.  
  34.             if((bp->cnt = fread(bp->data,1,BLKSIZE,fp)) == 0){
  35.                 free_p(bp);
  36.                 break;
  37.             }
  38.             total += bp->cnt;
  39.             if(send_mbuf(s,bp,0,NULLCHAR,0) == -1){
  40.                 return -1;
  41.             }
  42.             while(hash == V_HASH && total >= hmark+1000){
  43.                 tputc('#');
  44.                 hmark += 1000;
  45.             }
  46.             while(hash == V_BYTE && total >= hmark+Tcp_mss){
  47.                 tprintf("%8ld\r", total);
  48.                 hmark += Tcp_mss;
  49.             }
  50.  
  51.         }
  52.         break;
  53.     case ASCII_TYPE:
  54.         oldf = setflush(s,-1);
  55.         /* Let the newline mapping code in usputc() do the work */
  56.         while((c = fgetc(fp)) != EOF){
  57. #if !defined(UNIX) && !defined(__TURBOC__) && !defined(__TURBOC__)
  58.             if(c == '\r'){
  59.                 /* Needed only if the OS uses a CR/LF
  60.                  * convention and fgetc doesn't do
  61.                  * an automatic translation
  62.                  */
  63.                 continue;
  64.             }
  65. #endif
  66.             usputc(s,(char)c);
  67.             total++;
  68.             while(hash == V_HASH && total >= hmark+1000){
  69.                 tputc('#');
  70.                 hmark += 1000;
  71.             }
  72.             while(hash == V_BYTE && total >= hmark+Tcp_mss){
  73.                 tprintf("%8ld\r", total);
  74.                 hmark += Tcp_mss;
  75.             }
  76.         }
  77.         usflush(s);
  78.         setflush(s,oldf);        
  79.         break;
  80.     }
  81.     return total;
  82. }
  83. long
  84. recvfile(fp,s,mode,hash)
  85. FILE *fp;
  86. int s;
  87. int mode;
  88. int hash;
  89. {
  90.     int cnt,c;
  91.     struct mbuf *bp;
  92.     long total = 0;
  93.     long hmark = 0;
  94.  
  95.     if(hash == V_BYTE)
  96.         tprintf("         : Bytes Received\r");
  97.     switch(mode){
  98.     default:
  99.     case LOGICAL_TYPE:
  100.     case IMAGE_TYPE:
  101.         while((cnt = recv_mbuf(s,&bp,0,NULLCHAR,0)) != 0){
  102.             if(cnt == -1)
  103.                 return -1;
  104.  
  105.             total += cnt;
  106.             while(hash == V_HASH && total >= hmark+1000){
  107.                 tputc('#');
  108.                 hmark += 1000;
  109.             }
  110.             while(hash == V_BYTE && total >= hmark+Tcp_mss){
  111.                 tprintf("%8ld\r", total);
  112.                 hmark += Tcp_mss;
  113.             }
  114.             if(fp != NULLFILE){
  115.                 if(write_p(fp,bp) == -1){
  116.                     free_p(bp);
  117.                     return -1;
  118.                 }
  119.                 free_p(bp);
  120.             } else {
  121.                 send_mbuf(Curproc->output, bp, 0, NULLCHAR, 0);
  122.             }
  123.         }
  124.         break;
  125.     case ASCII_TYPE:
  126.         while((c = recvchar(s)) != EOF){
  127.             if(fp != NULLFILE){
  128. #if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
  129.                 if(c == '\n'){
  130.                     /* Needed only if the OS uses a CR/LF
  131.                      * convention and fputc doesn't do
  132.                      * an automatic translation
  133.                      */
  134.                     fputc('\r',fp);
  135.                 }
  136. #endif
  137.                 if(fputc(c,fp) == EOF){
  138.                     total = -1;
  139.                     break;
  140.                 }
  141.             } else {
  142.                 tputc((char)c);
  143.             }
  144.             total++;
  145.             while(hash == V_HASH && total >= hmark+1000){
  146.                 tputc('#');
  147.                 hmark += 1000;
  148.             }
  149.             while(hash == V_BYTE && total >= hmark+Tcp_mss){
  150.                 tprintf("%8ld\r", total);
  151.                 hmark += Tcp_mss;
  152.             }
  153.         }
  154.         break;
  155.     }
  156.     return total;
  157. }
  158.  
  159. /* Determine if a file appears to be binary (i.e., non-text).
  160.  * Return 1 if binary, 0 if ascii text after rewinding the file pointer.
  161.  *
  162.  * Used by FTP to warn users when transferring a binary file in text mode.
  163.  */
  164. int
  165. isbinary(fp)
  166. FILE *fp;
  167. {
  168.     int c,i;
  169.     int rval;
  170.  
  171.     rval = 0;
  172.     for(i=0;i<512;i++){
  173.         if((c = getc(fp)) == EOF)
  174.             break;
  175.         if(c & 0x80){
  176.             /* High bit is set, probably not text */
  177.             rval = 1;
  178.             break;
  179.         }
  180.     }
  181.     /* Assume it was at beginning */
  182.     fseek(fp,0L,0);
  183.     return rval;
  184. }
  185.